package com.example.demo.controller;

import com.example.demo.entity.*;
import com.example.demo.service.CarService;
import com.example.demo.service.GoodsService;
import com.example.demo.service.OrderService;
import com.example.demo.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import javax.jws.soap.SOAPBinding;
import java.util.ArrayList;
import java.util.List;

@Controller
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    GoodsService goodsService;
    @Autowired
    CarService carService;
    @Autowired
    UserService userService;

    @CrossOrigin
    @PostMapping(value = "/api/findorder")
    @ResponseBody
    public Result findorder(@RequestBody Order o)
    {
        List<Order> orders = orderService.findOrders(o.getUserid());

        Goods goods;

        for(int i = 0 ; i < orders.size() ; i++)
        {
           goods = goodsService.findGoodsById(orders.get(i).getGoodsid());
           orders.get(i).setGoods(goods);
        }
        Result result = new Result(200);
        result.setData(orders);
        System.out.println(result.getData().toString());
        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/deleteOrder")
    @ResponseBody
    public Result deleteOrder(@RequestBody Order o)
    {
        if(o.getStatus() == 1)
        {
            Goods tmp = new Goods();
            tmp.setGoodsid(o.getGoodsid());
            tmp.setNum(o.getNum());
            Type tmp1 = new Type();
            tmp1.setType(Integer.parseInt(goodsService.findGoodsById(o.getGoodsid()).getType()));
            tmp1.setVolume(-o.getNum());
            goodsService.updateVolume(tmp1);
            goodsService.updateGoodsNum(tmp);
        }

        orderService.deleteOrder(o);

        Result result = new Result(200);

        return result;
    }


    @CrossOrigin
    @PostMapping(value = "/api/insertOrder")
    @ResponseBody
    public Result insertOrder(@RequestBody Order o)
    {
        carService.removeFromCar(o.getGoodsid(),o.getUserid());
        orderService.insertOrder(o);
        Goods tmp = new Goods();
        tmp.setGoodsid(o.getGoodsid());
        tmp.setNum(-o.getNum());

        Type tmp1 = new Type();
        tmp1.setType(Integer.parseInt(goodsService.findGoodsById(o.getGoodsid()).getType()));
        tmp1.setVolume(o.getNum());
        goodsService.updateVolume(tmp1);
        goodsService.updateGoodsNum(tmp);
        Result result = new Result(200);
        result.setData(o.getOrderid());

        return result;
    }

    @CrossOrigin
    @PostMapping(value = "/api/insertOrderAndId")
    @ResponseBody
    public Result insertOrderAndId(@RequestBody Order o)
    {
        System.out.println(o.getOrderid());
        carService.removeFromCar(o.getGoodsid(),o.getUserid());
        orderService.insertOrderAndId(o);
        Goods tmp = new Goods();
        tmp.setGoodsid(o.getGoodsid());
        tmp.setNum(-o.getNum());
        Type tmp1 = new Type();
        tmp1.setType(Integer.parseInt(goodsService.findGoodsById(o.getGoodsid()).getType()));
        tmp1.setVolume(o.getNum());
        goodsService.updateVolume(tmp1);
        goodsService.updateGoodsNum(tmp);
        Result result = new Result(200);
        return result;
    }


    /*
    num 数量
    goodsid 商品编号
    message 退货理由
    statue 状态
     */
    @CrossOrigin
    @PostMapping(value = "/api/changeStatus")
    @ResponseBody
    public Result changeStatus(@RequestBody Order o)
    {
        System.out.println(o.toString());
        if(o.getStatus() == -3)
        {
            Goods goods = goodsService.findGoodsById(o.getGoodsid());
            List<Order> orders = orderService.findOrders(o.getOrderid());
            Goods tmp = new Goods();
            tmp.setGoodsid(o.getGoodsid());
            tmp.setNum(o.getNum());
            Type tmp1 = new Type();
            tmp1.setType(Integer.parseInt(goodsService.findGoodsById(o.getGoodsid()).getType()));
            tmp1.setVolume(-o.getNum());
            goodsService.updateVolume(tmp1);
            userService.addMoney(orders.get(0).getUserid(),goods.getNum()*goods.getPrice());
            goodsService.updateGoodsNum(tmp);

        }
        orderService.changeStatus(o);
        Result result = new Result(200);
        System.out.println(o.toString());
        return result;
    }


    @CrossOrigin
    @PostMapping(value = "/api/payBill")
    @ResponseBody
    public Result payBill(@RequestBody Order requestOrder)
    {
        User user = userService.findById(requestOrder.getUserid());
        userService.addMoney(requestOrder.getUserid(),user.getPoint()-requestOrder.getTotle_price());
        user = userService.findById(requestOrder.getUserid());
        Result result = new Result(200);
        result.setData(user.getPoint());
        return result;
    }

    @CrossOrigin
    @GetMapping(value = "/api/listOrder")
    @ResponseBody
    public OrderResult listOrder(@RequestParam("currentPage") int currentPage,@RequestParam("status") int status){
        List<Order> orders = new ArrayList<>();
        orders = orderService.listOrder(currentPage,status);
        List<Goods> goodsList = new ArrayList<>();
        for(Order order : orders){
            Goods goods = goodsService.findGoodsById(order.getGoodsid());
            goodsList.add(goods);
        }
        List<Address> addresses = new ArrayList<>();
        for(Order order : orders){
            Address address = userService.findAddressById(order.getAddressid());
            addresses.add(address);
        }
        List<User> users = new ArrayList<>();
        for(Order order : orders){
            User user = userService.findById(order.getUserid());
            users.add(user);
        }
        OrderResult result = new OrderResult();
        int total = orderService.listOrderTotal(status);
        result.setData1(orders);
        result.setData2(goodsList);
        result.setData3(addresses);
        result.setData4(users);
        result.setTotal(total);
        return result;
    }


    @CrossOrigin
    @GetMapping(value = "/api/getOrderData")
    @ResponseBody
    public Result getOrderData()
    {
        List<OrderData> orderDataList = new ArrayList<>();
        for(int i=-2;i<=3;i++){
            OrderData orderData = new OrderData();
            int value = orderService.listOrderTotal(i);
            orderData.setValue(value);
            switch (i){
                case -2:orderData.setName("退单完成");
                break;
                case -1:orderData.setName("退单申请中");
                break;
                case 0:orderData.setName("未付款");
                break;
                case 1:orderData.setName("未发货");
                break;
                case 2:orderData.setName("已发货");
                break;
                case 3:orderData.setName("已收货");
            }
            orderDataList.add(orderData);
        }
        Result result = new Result(200);
        result.setData(orderDataList);
        return result;
    }

}
